package org.example.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.example.dao.user.UserMapper;
import org.example.entity.PageBean;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Value("${wexin.appid}")
    String appid;
    @Value("${wexin.secret}")
    String secret;
    @Resource
    private UserMapper userMapper;

    /**
     * 获取整个表
     * */
    @Override
    public List<User> list() {
        return userMapper.list();
    }
    /**
     * 删除指定id
     * */
    @Override
    public boolean delete(Integer userId) {
        return userMapper.delete(userId);
    }
    /**
     * 添加用户
     * */
    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    /**
     * 更新指定用户信息
     * */
    @Override
    public void update(User user, int i) {
        user.setUserId(i);
        userMapper.update(user);
    }

    /**
     * 分页查询手动实现
     * */
    @Override
    public List<User> listByPage(int pageSize, int page) {

        return userMapper.listByPage(pageSize,(page - 1) * pageSize);
    }
    /**
     * 获取数据库用户总数
     * */
    @Override
    public int listSize() {
        return userMapper.listSize();
    }

    /**
     * 批量删除
     * */
    @Override
    public void deleteList(String[] userId) {
        for (int i = 0; i < userId.length; i++) {
            userMapper.delete(Integer.parseInt(userId[i]));
        }
    }

    /**
     * 通过PageHelper插件获取分页结果
     * 并按照条件进行查询
     * */
    @Override
    public PageBean<User> userByPage(int pageSize, int page,Integer userId,String username,
                               String gender,String phone,String name) {
        PageHelper.startPage(page,pageSize);
        List<User> list = userMapper.listByReason(userId,username,gender,phone,name);

        Page<User> p = (Page<User>) list;

        long total = p.getTotal();
        list = p.getResult();
        return new PageBean<User>(total,list);
    }

    @Override
    public List<String> login(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=" + appid +
                "&secret=" + secret +
                "&js_code=" + code +
                "&grant_type=authorization_code";
        System.out.println(url);
        RestTemplate restTemplate = new RestTemplate();
        String jsonData = restTemplate.getForObject(url, String.class);
        System.out.println(jsonData);
        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        String openid = jsonObject.getString("openid");
        String sessionKey = jsonObject.getString("session_key");

        List<String> list = new ArrayList<>();
        list.add(openid);
        list.add(sessionKey);

        System.out.println(list.toString());
        return list;
    }

    @Override
    public User loginUser(User user) {
        User login = userMapper.getUserByIdAndPassword(user);
        return login;
    }
}
